package com.agency8x.controller;

import com.agency8x.bean.SearchParamBean;
import com.agency8x.interceptor.CheckLoginInterceptor;
import com.agency8x.model.AgencyModel;
import com.agency8x.model.BankcardModel;
import com.agency8x.model.LoginLogModel;
import com.agency8x.model.RechargeModel;
import com.agency8x.model.TransactionDetailModel;
import com.agency8x.model.WithdrawModel;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 * 代理页控制器
 *
 * @author SoChishun
 *
 */
@Before(CheckLoginInterceptor.class)
public class AgencyController extends Controller {
	
	// 获取和设置查询参数
	private SearchParamBean getAndSetQueryParams(){
		SearchParamBean bean=new SearchParamBean();
		bean.setWord(getPara("word", ""));
		bean.setDate1(getPara("date1", ""));
		bean.setDate2(getPara("date2", ""));
		this.setAttr("word", bean.getWord());
		this.setAttr("date1", bean.getDate1());
		this.setAttr("date2", bean.getDate2());
		return bean;
	}

	public void index() {
		SearchParamBean search=getAndSetQueryParams();
		this.setAgentInfo();
		setAttr("listPage", AgencyModel.me.paginate(getParaToInt(0, 1), 10, search.getWord(), search.getDate1(), search.getDate2(), 0));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString());
		this.render("index.html");
	}

	/**
	 * 今日新增
	 */
	public void todaylist() {
		SearchParamBean search=getAndSetQueryParams();
		this.setAgentInfo();
		setAttr("listPage", AgencyModel.me.paginate(getParaToInt(0, 1), 10, search.getWord(), search.getDate1(), search.getDate2(), 1));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString());
		this.render("todaylist.html");
	}

	/**
	 * 本周新增
	 */
	public void weeklist() {
		SearchParamBean search=getAndSetQueryParams();
		this.setAgentInfo();
		setAttr("listPage", AgencyModel.me.paginate(getParaToInt(0, 1), 10, search.getWord(), search.getDate1(), search.getDate2(), 2));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString());
		this.render("weeklist.html");
	}

	/**
	 * 30天未登录
	 */
	public void unlogin30() {
		SearchParamBean search=getAndSetQueryParams();
		this.setAgentInfo();
		setAttr("listPage", AgencyModel.me.paginate(getParaToInt(0, 1), 10, search.getWord(), search.getDate1(), search.getDate2(), 3));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString());
		this.render("unlogin30.html");
	}

	private void setAgentInfo() {
		Integer user_id = this.getParaToInt("user_id", 0);
		this.setAttr("user_id", user_id);
		setAttr("itemData", AgencyModel.me.agenEdit(user_id));
	}

	/**
	 * 1级代理
	 */
	public void childagent1() {
		SearchParamBean search=getAndSetQueryParams();
		Integer user_id = this.getParaToInt("user_id", 0);
		this.setAgentInfo();
		setAttr("listPage", AgencyModel.me.getAgent1Pagination(getParaToInt(0, 1), 10, user_id, search.getWord(),search.getDate1(), search.getDate2(), 1));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString() + "&user_id=" + user_id);
		// 累计收入、今日收入
		//double incomeAmountTotal=AgencyModel.me.getAgent1IncomeAmountTotal(user_id);
		//double incomeAmountToday=AgencyModel.me.getAgent1IncomeAmountToday(user_id);
		//this.setAttr("incomeAmountTotal", incomeAmountTotal);
		//this.setAttr("incomeAmountToday", incomeAmountToday);
		this.render("childagent1.html");
	}

	/**
	 * 2级代理
	 */
	public void childagent2() {
		SearchParamBean search=getAndSetQueryParams();
		Integer user_id = this.getParaToInt("user_id", 0);
		this.setAgentInfo();
		setAttr("listPage", AgencyModel.me.parent1inate(getParaToInt(0, 1), 10, user_id, search.getWord(),search.getDate1(), search.getDate2(), 2));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString() + "&user_id=" + user_id);
		this.render("childagent2.html");
	}

	/**
	 * 银行卡
	 */
	public void bankcard() {
		this.setAgentInfo();
		Integer user_id = this.getParaToInt("user_id", 0);
		BankcardModel model = BankcardModel.me.findByUserId(user_id);
		this.setAttr("itemData1", model);
		this.render("bankcard.html");
	}


	/**
	 * 登录日志
	 */
	public void login_log() {
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        String word = this.getPara("word", ""); // 查询关键词
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAttr("word", word);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Page<LoginLogModel> list=LoginLogModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id);
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", "?word=" + word + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("login_log.html");
	}

    /**
     * 下线账户明细/日结账单
     */
    public void transaction_detail() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Page<TransactionDetailModel> list;
		switch (user_type) {
		case "c1":
			list = TransactionDetailModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = TransactionDetailModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = TransactionDetailModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString()+ "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("transaction_detail.html");
    }

    /**
     * 下线提现明细/提现记录
     */
    public void withdraw_pending_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer withdraw_status=0; // 提现状态
		Page<WithdrawModel> list;
		switch (user_type) {
		case "c1":
			list = WithdrawModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = WithdrawModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = WithdrawModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, withdraw_status,search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("withdraw_pending_list.html");
    }

    public void withdraw_success_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer withdraw_status=1; // 通过
		Page<WithdrawModel> list;
		switch (user_type) {
		case "c1":
			list = WithdrawModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = WithdrawModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = WithdrawModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("withdraw_success_list.html");
    }

    public void withdraw_rejected_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer withdraw_status=2; // 驳回
		Page<WithdrawModel> list;
		switch (user_type) {
		case "c1":
			list = WithdrawModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = WithdrawModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = WithdrawModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, withdraw_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("withdraw_rejected_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_online_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer recharge_status=1; // 通过
		Page<RechargeModel> list;
		switch (user_type) {
		case "c1":
			list = RechargeModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = RechargeModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = RechargeModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("recharge_online_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_direct_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer recharge_status=1; // 驳回
		Page<RechargeModel> list;
		switch (user_type) {
		case "c1":
			list = RechargeModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = RechargeModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = RechargeModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("recharge_direct_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_pending_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer recharge_status=1; // 通过
		Page<RechargeModel> list;
		switch (user_type) {
		case "c1":
			list = RechargeModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = RechargeModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = RechargeModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("recharge_pending_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_rejected_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer recharge_status=1; // 通过
		Page<RechargeModel> list;
		switch (user_type) {
		case "c1":
			list = RechargeModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = RechargeModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = RechargeModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("recharge_rejected_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_success_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer user_id = this.getParaToInt("user_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("user_id", user_id);
        this.setAgentInfo(); // 设置表格头部用户属性表
		Integer recharge_status=1; // 通过
		Page<RechargeModel> list;
		switch (user_type) {
		case "c1":
			list = RechargeModel.me.getPaginateByParent1Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		case "c2":
			list = RechargeModel.me.getPaginateByParent2Uid(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		default:
			list = RechargeModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, user_id, recharge_status, search.getWord(),search.getDate1(),search.getDate2());
			break;
		}
		setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&user_id=" + user_id);
        this.render("recharge_success_list.html");
    }


	/**
	 * 编辑
	 */
	public void edit() {
		Integer id = this.getParaToInt();
		setAttr("itemData", null == id || id < 1 ? null : AgencyModel.me.agenEdit(id));
		this.render("edit.html");
	}

	/**
	 * 保存
	 */
	public void save() {
		this.renderText("save!!!");
	}

	/**
	 * 个人资料
	 */
	public void profile() {
		this.render("profile.html");
	}

	/**
	 * 个人资料保存
	 */
	public void save_profile() {
		this.renderText("save!!!");
	}
}
